package com.yuncheng.spcyApi.service.impl;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.entity.SpcySqsxRcjg;
import com.yuncheng.spcyApi.mapper.SpcySqsxRcjgMapper;
import com.yuncheng.spcyApi.service.ISpcySqsxRcjgService;
import com.yuncheng.spcyApi.utils.DateConvert;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author hua
 */
@Service
public class SpcySqsxRcjgServiceImpl extends BaseServiceImpl<SpcySqsxRcjgMapper, SpcySqsxRcjg> implements ISpcySqsxRcjgService {

    private static final Logger log = LoggerFactory.getLogger(SpcySqsxRcjgServiceImpl.class);

    @Resource
    private SpcySqsxRcjgMapper spcySqsxRcjgMapper;

    /** 同步更新日常监管信息 */
    @Override
    public void syncUpdateRcjg(String sqsxid, String fxzspzt, String fjclx, HashMap<String,Object> map){

        if (StringUtils.isBlank(sqsxid) || map == null || map.isEmpty()){
            return;
        }
        if (StringUtils.isBlank(fxzspzt) || !fxzspzt.equals(SpcyConstant.SQSX_BASIC_XZSPZT_RCJC)){
            return;
        }
        /*if (StringUtils.isBlank(fjclx) || !fjclx.equals(SpcyConstant.SQSX_BASIC_FJCLX_ZXJC)){
            return;
        }*/

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            SpcySqsxRcjg rcjgBean = spcySqsxRcjgMapper.selectById(sqsxid);
            if (rcjgBean == null){
                rcjgBean = new SpcySqsxRcjg();
                rcjgBean.setId(sqsxid);

                save(rcjgBean);
            }

            BeanUtils.populate(rcjgBean, map);

            rcjgBean.setId(sqsxid);
            updateById(rcjgBean);

        }catch (Exception e){
            log.error("同步更新日常监管信息-错误: {}", e.getMessage());
        }
    }

    @Override
    public SpcySqsxRcjg selectRcjgById(String id, String fxzspzt, String fjclx, SpcySqsxBasic sqsx) {
        if (StringUtils.isBlank(id)){
            return null;
        }
        if (sqsx != null) {
            fxzspzt = sqsx.getFxzspzt();
        }

        if (StringUtils.isNotBlank(fxzspzt) && fxzspzt.equals(SpcyConstant.SQSX_BASIC_XZSPZT_RCJC)) {
            return getById(id);
        }

        return new SpcySqsxRcjg();
    }

}
